﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GL = CSharpGL.OpenGL;
using CSharpGL;
using System.Threading;

namespace ZOpenGLControl
{
    public partial class LineControl : UserControl
    {

        private List<DataItem>[] dataItemsArr;//数据
        private int[] colors;//线条颜色
        private bool showTime = true;//是否显示时间

        private List<DataItem>[] dataItemsArrCache;
        private bool isPause = false;//是否暂停状态

        public LineControl()
        {
           
            dataItemsArr = new List<DataItem>[8];

            for(var i = 0; i < dataItemsArr.Length; i++)
            {
                dataItemsArr[i] = new List<DataItem>();
            }

            InitializeComponent();

            hScrollBar1.Visible = false;
        }

        public void setColor(int[] colors)
        {
            this.colors = colors;
        }

        private void LineControl_Load(object sender, EventArgs e)
        {
        
        }


        private float vx(float num)
        {
            //-1,-1f
            return num / 600 - 1F + 0.1f;
        }

        private float vy(float num)
        {
            //-1,-1f
            return num / 600 - 1F + 0.2f;
        }
        private float vnx(float num)
        {
            return vx((num) / 1000 * 900+ 50);
        }
        private float vny(float num)
        {

            return vy((num) / 1000 * 900 + 50);
        }
        private int vtx(float num)
        {

            return (int)((glCanvas1.Width / 2 ) * (vx(num)+1));
        }
        private int vty(float num)
        {
            return (int)((glCanvas1.Height / 2) * (vy(num) + 1));

        }



        //private double rotation = 0;
        private void glCanvas1_OpenGLDraw(object sender, PaintEventArgs e)
        {
            OpenGL.ClearColor = Color.FromArgb(40, 44, 61);

            OpenGL.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
            OpenGL.LoadIdentity();

            //   GL.Rotate(rotation+=3, 0.0f, 1.0f, 0.0f);

          

            //边框
            GL.Color3f(0, 0, 1);
            GL.LineWidth(1);
            GL.Begin(OpenGL.GL_LINE_STRIP);
            GL.Vertex2f(vx(0), vy(0));
            GL.Vertex2f(vx(1000), vy(0));
            GL.Vertex2f(vx(1000), vy(1000));
            GL.Vertex2f(vx(0), vy(1000));
            GL.Vertex2f(vx(0), vy(0));
            GL.End();

      


            //刻度线
            for (var i = 0; i < 6; i++)
            {

                GL.Begin(GL.GL_LINES);
                GL.Color3f(0, 0, 1);
                GL.Vertex2f(vnx(0),vny(i*200));
                GL.Vertex2f(vnx(1000), vny(i*200));
                GL.End();


                int num = i * 40;
                String numTxt = num + "";

                int txtX = vtx(50) - numTxt.Length * 10;
                int txtY = vty(0 + 180 * i + 50) - 6;

                GL.DrawText(txtX, txtY, Color.White, "", 12, numTxt);


                int txtX2 = vtx(950) + 10;
                int txtY2 = txtY;
                String numTxt2 = num / 10 + "";
                GL.DrawText(txtX2, txtY2, Color.White, "", 12, numTxt2);


            }

         
            GL.LineWidth(5);
            GL.Enable(GL.GL_LINE_SMOOTH);

            GL.Enable(GL.GL_BLEND);
            //折线图

            for (var i0 = 0; i0 < dataItemsArr.Length; i0++)
            {
            
                var dataItems = dataItemsArr[i0];

                GL.Begin(GL.GL_LINE_STRIP);

               int color = colors[0];
               int red = (color & 0x00ff0000) >> 16;
               int green = (color & 0x0000ff00) >> 8;
               int blue = (color & 0x000000ff);
               GL.Color3f(red / 255, green / 255, blue / 255);


                int xx = 0;
                int w = 1000;

                if (isPause)
                {

                    int start = (int)(hScrollBar1.Value / 100.0 * dataItems.Count);
                    int end = 0;
                    if (start + w > dataItems.Count)
                    {
                        end = dataItems.Count;
                        start = end - w;
                    }
                    else
                    {
                        end = start + w;
                    }

                    for(var i = start; i < end; i++)
                    {
                        if (i < 0)
                        {
                            continue;
                        }
                        var dataItem = dataItems[i];

                        float x = vnx(xx++);
                        float y = vny((float)dataItem.value * 2.5F);
                        GL.Vertex2f(x, y);
                    }


                }
                else
                {
                    
                    for (var i = dataItems.Count - dataItems.Count % w; i < dataItems.Count; i++)
                    {

                        var dataItem = dataItems[i];
                        if (dataItem == null)
                        {
                            continue;
                        }
                        float x = vnx(xx++);
                        float y = vny((float)dataItem.value * 2.5F);
                        GL.Vertex2f(x, y);
                    }


                    int startIndex = dataItems.Count - w;
                    int endIndex = dataItems.Count - dataItems.Count % w;
                    for (var i = startIndex; i < endIndex; i++)
                    {
                        if (i < 0)
                        {
                            break;
                        }
                        var dataItem = dataItems[i];
                        float x = vnx(xx++);
                        float y = vny((float)dataItem.value * 2.5F);
                        GL.Vertex2f(x, y);

                    }

                }
                GL.End();
            }


            //右边计算数值
            int top = glCanvas1.Height - 100;
            int left = glCanvas1.Width - 80;

            GL.DrawText(left, top, Color.Red, "", 20, "(75)");
            GL.DrawText(left+4, top-15, Color.Red, "", 10, "Pa mean");

            GL.DrawText(left, top-80, Color.Green, "", 20, "(63)");
            GL.DrawText(left+4, top - 80-15, Color.Green, "", 10, "Pd mean");

            GL.DrawText(left, top - 80 - 80, Color.Yellow, "", 20, "0.84");
            GL.DrawText(left+13, top - 80 - 80 - 15, Color.Yellow, "", 10, "FFR");

            if (showTime)
            {
                //右上角时间显示
                GL.DrawText(glCanvas1.Width - 200, glCanvas1.Height - 20, Color.White, "", 12, DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
            }

            //    GL.Rectf(-0.9f, -0.9f, 10, 10);
            OpenGL.DrawText(50, glCanvas1.Height - 30, Color.Red, "", 14, "FPS:" + glCanvas1.FPS.ToString());

        }



        public void reset()
        {

            for(var i = 0; i < dataItemsArr.Length; i++)
            {
                dataItemsArr[i].Clear();
            }
        }

        public void pushData(DataItem item)
        {
            if (isPause)
            {
                dataItemsArrCache[item.type - 1].Add(item);
            }
            else
            {
                dataItemsArr[item.type - 1].Add(item);
            }
        }


    

        public void pause()
        {

            dataItemsArrCache = new List<DataItem>[8];

            for (var i = 0; i < dataItemsArrCache.Length; i++)
            {
                dataItemsArrCache[i] = new List<DataItem>();
            }

            isPause = true;
            hScrollBar1.Visible = true;
            hScrollBar1.Value = hScrollBar1.Maximum;
        }

        public void load(List<DataItem> list)
        {
            foreach(var dataItem in list)
            {
                pushData(dataItem);
            }
            pause();
            hScrollBar1.Visible = false;
            btn_pause.Visible = false;
            showTime = false;
        }


        public void continu()
        {
            isPause = false;

            for(var i = 0; i < dataItemsArr.Length; i++)
            {
                dataItemsArr[i].AddRange(dataItemsArrCache[i]);
            }
            dataItemsArrCache = null;

            hScrollBar1.Visible = false;
          
        }
     
        private void btn_pause_Click(object sender, EventArgs e)
        {
            if (isPause)
            {
                this.continu();
                btn_pause.Text = "暂停";
            }
            else
            {
                this.pause();
                btn_pause.Text = "继续";
            }
        }
    }
}
